package com.douqu.game.core.entity.db;

import com.bean.core.buffer.ByteBuffer;
import com.douqu.game.core.config.task.TaskConfig;
import com.douqu.game.core.factory.DataFactory;
import com.douqu.game.core.factory.TaskFactory;
import com.douqu.game.core.protobuf.SGCommonProto;

/**
 * @author: Gavin. Description: Date: 2017/10/25 0025 下午 12:32 Huan Yu Copyright
 * (c) 2017 All Rights Reserved.
 */
public class TaskDB extends DB {

    public int currentDemand;

    public int status;

    public TaskDB() {
        super(DataFactory.TASK_KEY);
    }

    public TaskDB(int id) {
        super(DataFactory.TASK_KEY, id);
        this.status = SGCommonProto.E_TASK_STATUS.TASK_STATUS_UNDERWAY_VALUE;
    }

    @Override
    public void writeTo(ByteBuffer byteBuffer) {
        byteBuffer.writeInt(id);
        byteBuffer.writeInt(currentDemand);
        byteBuffer.writeByte(status);
    }

    @Override
    public void loadFrom(ByteBuffer byteBuffer) {
        id = byteBuffer.readInt();
        currentDemand = byteBuffer.readInt();
        status = byteBuffer.readByte();
    }

    @Override
    public void reset() {

    }

    public void check(int type, int count) {
        if (count == 0)
            return;
        if (status != SGCommonProto.E_TASK_STATUS.TASK_STATUS_UNDERWAY_VALUE)
            return;
        TaskConfig taskConfig = DataFactory.getInstance().getGameObject(DataFactory.TASK_KEY, id);
        if (type != taskConfig.target)
            return;

        boolean updateStatus = false;
        switch (type) {
            case TaskFactory.TASK_TARAGET_LOTTERY:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EQUIPINTENSIFY:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_MAINUPLV:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_PASSLEVEL:
                updateStatus = count == taskConfig.demand[0];
                if (updateStatus)
                    this.currentDemand = taskConfig.showNum;
                break;
            case TaskFactory.TASK_TARAGET_ARENA:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_GUBLIANSTORE:
                this.currentDemand += count;
                updateStatus = currentDemand == taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_CARD:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_MAIN_ARENA_BATTLE:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_INSTANCE:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_GUBLIANSTORE:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_MAIN_EQUIPINTENSIFY:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_MAIN_CARD_LV:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_CARD_LV:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_MAIN_CARD_STAR:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_MAIN_OFFICIALWAR:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_MAIN_ACCESSORY_INTENSIFY:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_MAIN_EQUIPSYN:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_BATTLE_FC:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_REBORN:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERY_ALTAR:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_LOTTERY:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_YUAN_ZHENG:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_PROPERTIE_STONE:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_EXP_JITAN:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_GOLD_JITAN:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_SHOP_BUY:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_MAJOR:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_EQUIP_JITAN:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_SHIPIN_QIANGHUA:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_SHIPIN_JINTAN:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_YAOSHUI_JITAN:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_WORLDMAP_BATTLE_WIN:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_WORLDMAP_KILL_HURT:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_WORLDMAP_OCCUPY_CITY:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_SYNC:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_SHIPIN:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_SHIPIN_SHENGJIE:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_HERO_STAR:
                if (count <= this.currentDemand) {
                    break;
                }
                this.currentDemand = count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
            case TaskFactory.TASK_TARAGET_EVERYDAY_PROPERTIE:
                this.currentDemand += count;
                updateStatus = currentDemand >= taskConfig.demand[0];
                break;
        }
        this.currentDemand = this.currentDemand > taskConfig.demand[0] ? taskConfig.demand[0]
                : this.currentDemand;
        if (updateStatus) {
            status = SGCommonProto.E_TASK_STATUS.TASK_STATUS_UNCLAIMED_VALUE;
            // mainTaskPush(taskConfig.type,taskConfig.id,playerController);
        }

    }

    @Override
    public TaskConfig getConfig() {
        return (TaskConfig) super.getConfig();
    }

    @Override
    public String toString() {
        return "TaskData{" + "id=" + id + ", currentDemand=" + currentDemand + ", status=" + status + '}';
    }
}
